import 'package:flutter/material.dart';
import './routers/index.dart';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import './provider/index.dart';
import './provider/vipPayment.dart';
import './provider/forum.dart';

void main(List<String> args) async {
  WidgetsFlutterBinding.ensureInitialized();
  var globalData = GlobalData();
  var vippayment = VipPayment();
  var forum = Forum();
  var loginShow = await globalData.initUser();
  // token != null ? globalData.setToken(token) : '';
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => globalData),
        ChangeNotifierProvider(create: (_) => vippayment),
        ChangeNotifierProvider(create: (_) => forum),
      ],
      child: MyApp(loginShow),
    ),
  );
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  var loginShow;
  MyApp(this.loginShow);

  @override
  _MyAppState createState() => _MyAppState(loginShow);
}

class _MyAppState extends State<MyApp> {
  var loginShow;
  _MyAppState(this.loginShow);
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1624),
      allowFontScaling: false,
      child: MaterialApp(
        theme: ThemeData(
          primaryColor: Colors.white,
        ),
        debugShowCheckedModeBanner: false,
        initialRoute: loginShow ? '/home' : '/login',
        // initialRoute: loginShow ? '/journaism' : '/login',
        routes: {...routers},
        builder: EasyLoading.init(),
      ),
    );
  }
}
